<html>
<head>
  <title>Evernote Export</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="YXBJ Windows/601748 (zh-CN, DDL); Windows/10.0.0 (Win64); EDAMVersion=V2;"/>
  <meta name="content-class" content="yinxiang.markdown"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="601"/>

<div><span><div style="font-size: 14px; margin: 0; padding: 0; width: 100%;"><h2 style="line-height: 160%; box-sizing: content-box; font-weight: 700; font-size: 34px; border-bottom: 1px solid #dbdbdb; color: #333;">特殊方法</h2>
<ul style="line-height: 160%; box-sizing: content-box; display: block; list-style-type: disc; padding-left: 30px; margin: 6px 0 10px; color: #333;">
<li style="line-height: 160%; box-sizing: content-box; position: relative;">用一个向量的例子来解释特殊方法</li>
<li style="line-height: 160%; box-sizing: content-box; position: relative;">特殊方法是在类中以双下划线开头和结尾的方法</li>
</ul>
<pre style="line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; margin: 2px 0 8px; background-color: #f5f7f8;"><code style="display: block; overflow-x: auto; background: #1e1e1e; line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; letter-spacing: -.3px; padding: 18px; color: #f4f4f4; white-space: pre-wrap;"><span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">from</span> math <span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">import</span> hypot
<span style="color: #57a64a; font-style: italic; line-height: 160%; box-sizing: content-box;">#hypot返回阿基里德2范数</span>

<span style="color: #b8d7a3; line-height: 160%; box-sizing: content-box;"><span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">class</span> <span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;">Vector</span>:</span>
    <span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;"><span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">def</span> <span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;">__init__</span><span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;">(self,x = <span style="color: #b8d7a3; line-height: 160%; box-sizing: content-box;">0</span>,y = <span style="color: #b8d7a3; line-height: 160%; box-sizing: content-box;">0</span>)</span>:</span>
        self.x = x
        self.y = y
       
   <span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;"><span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">def</span> <span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;">__repr__</span><span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;">(self)</span>:</span>
        <span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">return</span> <span style="color: #d69d85; line-height: 160%; box-sizing: content-box;">'Vector(%r,%r)'</span> % (self.x , self.y)
   
   <span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;"><span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">def</span> <span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;">__abs__</span><span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;">(self)</span>:</span>
        <span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">return</span> hypot(self.x , self.y)
   
   <span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;"><span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">def</span> <span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;">__bool__</span><span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;">(self)</span>:</span>
        <span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">return</span> bool(abs(self))
   
   <span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;"><span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">def</span> <span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;">__add__</span><span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;">(self, other)</span>:</span>
        x = self.x +other.x
        y = self.y +other.y
        <span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">return</span> Vector(x,y)
   
   <span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;"><span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">def</span> <span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;">__mul__</span><span style="color: #dcdcdc; line-height: 160%; box-sizing: content-box;">(self, scalar)</span>:</span>
        <span style="color: #569cd6; line-height: 160%; box-sizing: content-box;">return</span> Vector(self.x * scalar , self.y * scalar)
</code></pre>
<blockquote style="line-height: 160%; box-sizing: content-box; margin: 15px 0; border-left: 4px solid #ddd; padding: 0 15px; color: #777;">
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333; margin-top: 0; margin-bottom: 0;"><strong style="line-height: 160%; box-sizing: content-box; font-weight: 700;">repr</strong><br/>
_ _ <em style="line-height: 160%; box-sizing: content-box; font-style: italic;">repr</em> _ _ 是“字符串表示形式”，如果没有实现repr，在控制台中打印一个向量的实例时，就只会返回它的内存地址，而没有返回内容</p>
</blockquote>
<blockquote style="line-height: 160%; box-sizing: content-box; margin: 15px 0; border-left: 4px solid #ddd; padding: 0 15px; color: #777;">
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333; margin-top: 0; margin-bottom: 0;"><strong style="line-height: 160%; box-sizing: content-box; font-weight: 700;">为什么len要做成特殊方法而不是普通方法？</strong><br/>
为了效率，写成特殊方法时，在调用len(x)时，实际上是读了内存中长度可变的内置对象的C语言结构体的一个属性，读取一个值比调用一个方法，远远快得多。</p>
</blockquote>
<blockquote style="line-height: 160%; box-sizing: content-box; margin: 15px 0; border-left: 4px solid #ddd; padding: 0 15px; color: #777;">
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333; margin-top: 0; margin-bottom: 0;"><strong style="line-height: 160%; box-sizing: content-box; font-weight: 700;">运算符重载</strong><br/>
就是把一些用于运算的方法用特殊方法覆写</p>
</blockquote>
<blockquote style="line-height: 160%; box-sizing: content-box; margin: 15px 0; border-left: 4px solid #ddd; padding: 0 15px; color: #777;">
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333; margin-top: 0; margin-bottom: 0;"><strong style="line-height: 160%; box-sizing: content-box; font-weight: 700;">元对象协议</strong><br/>
元对象协议就是对象模型，意思都是构建核心语言的API，协议即接口</p>
</blockquote>
</div><center style="display:none !important;visibility:collapse !important;height:0 !important;white-space:nowrap;width:100%;overflow:hidden">%23%23%20%E7%89%B9%E6%AE%8A%E6%96%B9%E6%B3%95%0A-%20%E7%94%A8%E4%B8%80%E4%B8%AA%E5%90%91%E9%87%8F%E7%9A%84%E4%BE%8B%E5%AD%90%E6%9D%A5%E8%A7%A3%E9%87%8A%E7%89%B9%E6%AE%8A%E6%96%B9%E6%B3%95%0A-%20%E7%89%B9%E6%AE%8A%E6%96%B9%E6%B3%95%E6%98%AF%E5%9C%A8%E7%B1%BB%E4%B8%AD%E4%BB%A5%E5%8F%8C%E4%B8%8B%E5%88%92%E7%BA%BF%E5%BC%80%E5%A4%B4%E5%92%8C%E7%BB%93%E5%B0%BE%E7%9A%84%E6%96%B9%E6%B3%95%0A%0A%60%60%60python%0Afrom%20math%20import%20hypot%0A%23hypot%E8%BF%94%E5%9B%9E%E9%98%BF%E5%9F%BA%E9%87%8C%E5%BE%B72%E8%8C%83%E6%95%B0%0A%0Aclass%20Vector%3A%0A%20%20%20%20def%20__init__(self%2Cx%20%3D%200%2Cy%20%3D%200)%3A%0A%20%20%20%20%20%20%20%20self.x%20%3D%20x%0A%20%20%20%20%20%20%20%20self.y%20%3D%20y%0A%20%20%20%20%20%20%20%0A%20%20%20def%20__repr__(self)%3A%0A%20%20%20%20%20%20%20%20return%20'Vector(%25r%2C%25r)'%20%25%20(self.x%20%2C%20self.y)%0A%20%20%20%0A%20%20%20def%20__abs__(self)%3A%0A%20%20%20%20%20%20%20%20return%20hypot(self.x%20%2C%20self.y)%0A%20%20%20%0A%20%20%20def%20__bool__(self)%3A%0A%20%20%20%20%20%20%20%20return%20bool(abs(self))%0A%20%20%20%0A%20%20%20def%20__add__(self%2C%20other)%3A%0A%20%20%20%20%20%20%20%20x%20%3D%20self.x%20%2Bother.x%0A%20%20%20%20%20%20%20%20y%20%3D%20self.y%20%2Bother.y%0A%20%20%20%20%20%20%20%20return%20Vector(x%2Cy)%0A%20%20%20%0A%20%20%20def%20__mul__(self%2C%20scalar)%3A%0A%20%20%20%20%20%20%20%20return%20Vector(self.x%20*%20scalar%20%2C%20self.y%20*%20scalar)%0A%60%60%60%0A%0A%3E%20__repr__%0A_%20_%20_repr_%20_%20_%20%E6%98%AF%E2%80%9C%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%A1%A8%E7%A4%BA%E5%BD%A2%E5%BC%8F%E2%80%9D%EF%BC%8C%E5%A6%82%E6%9E%9C%E6%B2%A1%E6%9C%89%E5%AE%9E%E7%8E%B0repr%EF%BC%8C%E5%9C%A8%E6%8E%A7%E5%88%B6%E5%8F%B0%E4%B8%AD%E6%89%93%E5%8D%B0%E4%B8%80%E4%B8%AA%E5%90%91%E9%87%8F%E7%9A%84%E5%AE%9E%E4%BE%8B%E6%97%B6%EF%BC%8C%E5%B0%B1%E5%8F%AA%E4%BC%9A%E8%BF%94%E5%9B%9E%E5%AE%83%E7%9A%84%E5%86%85%E5%AD%98%E5%9C%B0%E5%9D%80%EF%BC%8C%E8%80%8C%E6%B2%A1%E6%9C%89%E8%BF%94%E5%9B%9E%E5%86%85%E5%AE%B9%0A%0A%3E%20__%E4%B8%BA%E4%BB%80%E4%B9%88len%E8%A6%81%E5%81%9A%E6%88%90%E7%89%B9%E6%AE%8A%E6%96%B9%E6%B3%95%E8%80%8C%E4%B8%8D%E6%98%AF%E6%99%AE%E9%80%9A%E6%96%B9%E6%B3%95%EF%BC%9F__%0A%20%20%20%20%E4%B8%BA%E4%BA%86%E6%95%88%E7%8E%87%EF%BC%8C%E5%86%99%E6%88%90%E7%89%B9%E6%AE%8A%E6%96%B9%E6%B3%95%E6%97%B6%EF%BC%8C%E5%9C%A8%E8%B0%83%E7%94%A8len(x)%E6%97%B6%EF%BC%8C%E5%AE%9E%E9%99%85%E4%B8%8A%E6%98%AF%E8%AF%BB%E4%BA%86%E5%86%85%E5%AD%98%E4%B8%AD%E9%95%BF%E5%BA%A6%E5%8F%AF%E5%8F%98%E7%9A%84%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%E7%9A%84C%E8%AF%AD%E8%A8%80%E7%BB%93%E6%9E%84%E4%BD%93%E7%9A%84%E4%B8%80%E4%B8%AA%E5%B1%9E%E6%80%A7%EF%BC%8C%E8%AF%BB%E5%8F%96%E4%B8%80%E4%B8%AA%E5%80%BC%E6%AF%94%E8%B0%83%E7%94%A8%E4%B8%80%E4%B8%AA%E6%96%B9%E6%B3%95%EF%BC%8C%E8%BF%9C%E8%BF%9C%E5%BF%AB%E5%BE%97%E5%A4%9A%E3%80%82%0A%20%20%20%20%0A%3E%20__%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD__%0A%20%20%20%20%E5%B0%B1%E6%98%AF%E6%8A%8A%E4%B8%80%E4%BA%9B%E7%94%A8%E4%BA%8E%E8%BF%90%E7%AE%97%E7%9A%84%E6%96%B9%E6%B3%95%E7%94%A8%E7%89%B9%E6%AE%8A%E6%96%B9%E6%B3%95%E8%A6%86%E5%86%99%0A%0A%3E%20__%E5%85%83%E5%AF%B9%E8%B1%A1%E5%8D%8F%E8%AE%AE__%0A%20%20%20%E5%85%83%E5%AF%B9%E8%B1%A1%E5%8D%8F%E8%AE%AE%E5%B0%B1%E6%98%AF%E5%AF%B9%E8%B1%A1%E6%A8%A1%E5%9E%8B%EF%BC%8C%E6%84%8F%E6%80%9D%E9%83%BD%E6%98%AF%E6%9E%84%E5%BB%BA%E6%A0%B8%E5%BF%83%E8%AF%AD%E8%A8%80%E7%9A%84API%EF%BC%8C%E5%8D%8F%E8%AE%AE%E5%8D%B3%E6%8E%A5%E5%8F%A3</center></span>
</div></body></html> 